package com.stadium.web.service.impl;

import com.fastsite.common.enums.StatusEnum;
import com.fastsite.dao.utils.PageSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.stadium.web.repository.VenueRepository;
import com.stadium.web.domain.Venue;
import com.stadium.web.service.VenueService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 场馆Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-03-15
 */
@Service
public class VenueServiceImpl implements VenueService
{
    @Autowired
    private VenueRepository venueRepository;

    /**
     * 根据场馆ID查询场馆数据
     * @param id 场馆ID
     */
    @Override
    @Transactional
    public Venue getById(Long id) {
        return venueRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Venue> getPageList(Example<Venue> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest(null,Sort.Direction.ASC);
        return venueRepository.findAll(example, page);
    }


    /**
     * 保存场馆
     * @param venue 场馆 实体类
     */
    @Override
    public Venue save(Venue venue){
        return venueRepository.save(venue);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> ids){
        // 删除场馆时取消与场馆和菜单的关联
        return venueRepository.updateStatusByIdIn(statusEnum.getCode(), ids) > 0;
    }

    @Override
    public List<Venue> findByUserId(Long id) {
        return venueRepository.findByUserId(id);
    }

    @Override
    public Venue findByUserIdAndVenueId(Long userId, Long venueId) {
        return venueRepository.findByUserIdAndId(userId,venueId);
    }

    @Override
    public List<Venue> getVenueByRegionId(Long regionId) {
        return venueRepository.findByRegionId(regionId);
    }

}
